package work.xkong.bo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.xkong.common.Const;
import work.xkong.common.ResponseRest;
import work.xkong.entity.Admin;
import work.xkong.entity.Student;
import work.xkong.entity.Teacher;
import work.xkong.mapper.AdminMapper;
import work.xkong.mapper.StudentMapper;
import work.xkong.mapper.TeacherMapper;
import work.xkong.vo.inputvo.LoginInfoInputVo;
import work.xkong.vo.outputvo.LoginInfoOutputVo;

/**
 * @PACKAGE_NAME: work.xkong.bo
 * @NAME: LoginBO
 * @USER: NuanYang
 * @create: 2022/11/22 14:07
 *
 * 获取登录用户信息的BO
 */
@Service
public class LoginBO {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private AdminMapper adminMapper;

    public LoginInfoOutputVo getUser(LoginInfoInputVo loginInfoInput){
        Integer type = loginInfoInput.getType();

        if (type == Const.LoginInfo.STUDENT) {
            LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Student::getStudentNumber,loginInfoInput.getUsername());
            Student student = studentMapper.selectOne(lqw);

            return student == null ? null : new LoginInfoOutputVo(
                    student.getStudentId(),
                    student.getStudentName(),
                    student.getStudentPassword(),
                    type
            );
        }

        if (type == Const.LoginInfo.TEACHER){
            LambdaQueryWrapper<Teacher> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Teacher::getTeacherNumber,loginInfoInput.getUsername());
            Teacher teacher = teacherMapper.selectOne(lqw);

            return teacher == null ? null : new LoginInfoOutputVo(
                    teacher.getTeacherId(),
                    teacher.getTeacherName(),
                    teacher.getTeacherPassword(),
                    type
            );
        }

        if (type == Const.LoginInfo.ADMIN){
            LambdaQueryWrapper<Admin> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Admin::getAdminUsername,loginInfoInput.getUsername());
            Admin admin = adminMapper.selectOne(lqw);

            return admin == null ? null : new LoginInfoOutputVo(
                    admin.getAdminId(),
                    admin.getAdminUsername(),
                    admin.getAdminPassword(),
                    type
            );
        }

        return null;
    }
}
